package com.bnyy.medicalHousekeeper.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.Goods;
import com.bnyy.medicalHousekeeper.bean.NormalGoodsDetail;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.listener.OnPageChangeListener;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/7
 **/
public class NormalGoodsDetailActivity extends BaseActivityImpl {

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tv_indicator)
    TextView tvIndicator;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_banner_root)
    RelativeLayout rlBannerRoot;
    @BindView(R.id.tv_sell_price)
    TextView tvSellPrice;
    @BindView(R.id.tv_original_price)
    TextView tvOriginalPrice;
    @BindView(R.id.tv_sell_count)
    TextView tvSellCount;
    @BindView(R.id.tv_goods_name)
    TextView tvGoodsName;
    @BindView(R.id.tag_flow_layout)
    TagFlowLayout tagFlowLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_share)
    TextView tvShare;

    public static void show(Context context, Goods goods) {
        Intent intent = new Intent(context, NormalGoodsDetailActivity.class);
        intent.putExtra("goods", goods);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_normal_goods_detail;
    }

    @Override
    public String getTitleStr() {
        return null;
    }

    @Override
    public boolean unuseRefreshLayout() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Goods goods = (Goods) getIntent().getSerializableExtra("goods");

        HashMap<String, Object> params = new HashMap<>();
        params.put("shop_id", goods.getShop_id());
        params.put("goods_id", goods.getGoods_id());
        RequestManager requestManager = RequestManager.getInstance();
        requestManager.request(requestManager.mJavaRetrofitService.getNormalGoodsDetail(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<NormalGoodsDetail>() {
                    @Override
                    public void onSuccess(NormalGoodsDetail goodsDetail) {
                        super.onSuccess(goodsDetail);

                        setGoodsDetail(goodsDetail);
                    }
                });

        tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArticleListActivity.show(mContext, goods.getGoods_id(), goods.getType());
            }
        });
    }

    public void setGoodsDetail(NormalGoodsDetail goodsDetail) {

        tvGoodsName.setText(goodsDetail.getGoods_name());

        SpanUtils.with(tvOriginalPrice)
                .append("¥" + goodsDetail.getMarket_price()).setFontSize(14, true)
                .setStrikethrough()
                .create();

        SpanUtils.with(tvSellPrice)
                .append("¥").setFontSize(14, true)
                .append(goodsDetail.getSelling_price() + "").setFontSize(20, true).setBold()
                .create();
        tvSellCount.setText("已售");
        int sellCount = goodsDetail.getSell_count();
        DecimalFormat df = new DecimalFormat("0.0");
        if (sellCount > 10000) {
            tvSellCount.append(df.format(sellCount / 10000F) + "万+");
        } else if (sellCount > 1000) {
            tvSellCount.append(df.format(sellCount / 1000F) + "千+");
        } else {
            tvSellCount.append(sellCount + "");
        }
        tvSellCount.append("件");

        ArrayList<String> list = goodsDetail.getText_list();
        if (list != null && list.size() > 0) {
            tagFlowLayout.setAdapter(new TagAdapter<String>(list) {
                int margin = SizeUtils.dp2px(5);

                @Override
                public View getView(FlowLayout parent, int position, String s) {

                    ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    lp.rightMargin = margin;

                    TextView tv = new TextView(mContext);
                    tv.setLayoutParams(lp);
                    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                    tv.setPadding(10, 5, 10, 5);
                    GradientDrawable bg = new GradientDrawable();
                    bg.setCornerRadius(5);
                    if (s.equals("百年精选")) {
                        bg.setColors(new int[]{Color.parseColor("#FD8C6B"), Color.parseColor("#FF5241")});
                        tv.setTextColor(Color.WHITE);
                    } else if (s.equals("包邮")) {
                        bg.setColor(Color.parseColor("#FFE9E9"));
                        tv.setTextColor(Color.RED);
                    }
                    tv.setBackground(bg);
                    tv.setText(s);
                    return tv;
                }
            });
        }

        ArrayList<String> goodsImages = goodsDetail.getGoods_images();
        int width = ScreenUtils.getScreenWidth();
        banner.setLayoutParams(new RelativeLayout.LayoutParams(width, width / 15 * 13));
        banner.setAdapter(new BannerImageAdapter<String>(goodsImages) {
            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                Glide.with(mContext).load(GlideHelper.getGlideUrl(data)).centerCrop().into(holder.imageView);
            }
        });

        banner.isAutoLoop(false);

        GradientDrawable bg = new GradientDrawable();
        bg.setColor(Color.parseColor("#40000000"));
        bg.setCornerRadius(360);

        tvIndicator.setBackground(bg);
        tvIndicator.setTextColor(Color.WHITE);
        tvIndicator.setText("1 / " + goodsImages.size());

        banner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvIndicator.setText((position + 1) + " / " + goodsImages.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(new RecyclerView.Adapter<ViewHolder>() {

            @NonNull
            @Override
            public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new ViewHolder(new RelativeLayout(mContext));
            }

            @Override
            public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

                Glide.with(mContext)
                        .load(GlideHelper.getGlideUrl(goodsDetail.getGoods_detail_images().get(position)))
                        .into(new CustomTarget<Drawable>() {
                            @Override
                            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                                int minimumWidth = resource.getMinimumWidth();
                                int minimumHeight = resource.getMinimumHeight();

                                int screenWidth = ScreenUtils.getScreenWidth();

                                int width = Math.max(minimumWidth, screenWidth);
                                int height = width * minimumHeight / minimumWidth;

                                ImageView iv;

                                if (holder.rl.getChildCount() > 0) {
                                    iv = (ImageView) holder.rl.getChildAt(0);
                                } else {
                                    iv = new ImageView(mContext);
                                    holder.rl.addView(iv);
                                }
                                iv.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
                                iv.setImageDrawable(resource);
                            }

                            @Override
                            public void onLoadCleared(@Nullable Drawable placeholder) {

                            }
                        });
            }

            @Override
            public int getItemCount() {
                return goodsDetail.getGoods_detail_images().size();
            }

        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout rl;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            rl = (RelativeLayout) itemView;
        }
    }
}






















